<?php
/**
 * Created by PhpStorm.
 * User: ljc
 * Date: 2018/6/20
 * Time: 16:46
 */

namespace app\home\controller;
use houdun\core\Controller;
use system\model\Grade;
use system\model\Student as StudentModel;
class Student extends Controller
{
	public function index(){
		$sql='select s.id,sname,age,sex,class_id,name from student s join grade g on s.class_id=g.id';
		$data=StudentModel::q($sql);
		$data=array_chunk ($data,4,true);
		$page=isset($_GET['page'])?$_GET['page']:0;
		$count=count($data);
		$data=$data[$page];
		return view('',compact('data','count'));
	}
	public function edit(){
		$id=isset($_GET['id'])?$_GET['id']:header('Location:/');
		$sql='select s.id,sname,age,sex,class_id,name from student s join grade g on s.class_id=g.id where s.id='.$id;
		$data=current(StudentModel::q($sql));
		$grade=Grade::getAll();
		if(IS_POST){
			StudentModel::where("id={$id}")->update($_POST);
			$this->setRedirct ('?c=student&a=index')->message ('修改成功');
		}
		return view('',compact('data','grade'));
	}
	public function add(){
		$data=Grade::field('id,name')->getAll();
		if(IS_POST){
			StudentModel::insert($_POST);
			$this->setRedirct ('?c=student&a=index')->message ('添加成功');
		}
		return view('',compact('data'));
	}
	public function del(){
		$id=isset($_GET['id'])?$_GET['id']:header('Location:/');
		StudentModel::where("id={$id}")->del();
		$this->setRedirct ('?c=student&a=index')->message ('删除成功');
	}
}